package com.tools.web.work.excel.poi;

import com.tools.common.exception.DataGenerationException;
import com.tools.common.object.Note;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Map;
import java.util.Objects;

/**
 * 导入 Excel 时 Sheet 页的结构对象
 * */
@Note("导入 Excel 时 Sheet 页的结构对象")
public class SheetImportBuilder {

    @Note("表头配置类")
    private ExcelHead excelHead;

    @Note("单元格的索引与字段名称的 Map 集合")
    private Map<Integer, String> indexAndFieldMap;


    /* ************************************************************************************************************
     *
     *      构造器
     *
     * ************************************************************************************************************
     * */

    public SheetImportBuilder(ExcelHead excelHead) {
        this.setExcelHead(excelHead);
    }

    public static SheetImportBuilder of(ExcelHead excelHead) {
        return new SheetImportBuilder(excelHead);
    }

    /* ************************************************************************************************************
     *
     *      Getter / Setter
     *
     * ************************************************************************************************************
     * */

    public ExcelHead getExcelHead() {
        return excelHead;
    }

    public void setExcelHead(ExcelHead excelHead) {
        if(excelHead == null) throw new NullPointerException("Excel 表头配置为 null");
        this.excelHead = excelHead;
    }

    Map<Integer, String> getIndexAndFieldMap(Sheet sheet) {
        if(this.indexAndFieldMap == null) {
            this.setIndexAndFieldMap(sheet);
        }
        return this.indexAndFieldMap;
    }

    public void setIndexAndFieldMap(Sheet sheet) {
        if(sheet == null) {
            throw new NullPointerException("Sheet 对象不能为 null");
        }
        if(this.excelHead == null) {
            throw new DataGenerationException("Excel 表头配置为 null");
        }
        this.indexAndFieldMap = ExcelKit.readHeadAndFieldAsMap(sheet, this);
    }

    @Override
    public String toString() {
        return "SheetImportBuilder{" +
                ", excelHead=" + excelHead +
                ", indexAndFieldMap=" + indexAndFieldMap +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SheetImportBuilder that = (SheetImportBuilder) o;
        return Objects.equals(excelHead, that.excelHead);
    }

    @Override
    public int hashCode() {
        return Objects.hash(excelHead);
    }

    /* ************************************************************************************************************
     *
     *      链式调用
     *
     * ************************************************************************************************************
     * */

    public SheetImportBuilder excelHead(ExcelHead excelHead) {
        this.setExcelHead(excelHead);
        return this;
    }

    public SheetImportBuilder indexAndFieldMap(Sheet sheet) {
        this.setIndexAndFieldMap(sheet);
        return this;
    }
}
